<template>
  <div>
    <el-card class="mt-10">
      <el-form>
        <el-form-item>
          <el-button
            type="primary"
            :disabled="!categoryStore.category3Id"
            @click="isShowAddSpu"
            >添加SPU</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-table :data="spuList" border class="mt-20">
            <el-table-column label="序号" type="index" width="80" />
            <el-table-column label="SPU名称" prop="spuName" />
            <el-table-column label="SPU描述" prop="description" />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <el-tooltip effect="dark" content="添加SKU" placement="bottom">
                  <el-button
                    :icon="Plus"
                    type="primary"
                    size="small"
                    @click="isShowAddSku(row)"
                  />
                </el-tooltip>
                <el-tooltip effect="dark" content="修改SPU" placement="bottom"
                  ><el-button
                    :icon="Edit"
                    type="warning"
                    size="small"
                    @click="isShowUpdateSpu(row)"
                /></el-tooltip>
                <el-tooltip
                  effect="dark"
                  content="查看SPU列表"
                  placement="bottom"
                  ><el-button :icon="ZoomIn" type="primary" size="small" />
                </el-tooltip>
                <el-tooltip
                  effect="dark"
                  content="删除SPU"
                  placement="bottom"
                  :icon="Delete"
                  ><el-button :icon="Delete" type="primary" size="small"
                /></el-tooltip>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item>
          <el-pagination
            class="mt-20"
            v-model:currentPage="currentPage"
            v-model:page-size="pageSize"
            :page-sizes="[3, 6, 9, 12]"
            layout="prev, pager, next, jumper, ->, sizes, total"
            :total="total"
            @size-change="getSpuList"
            @current-change="getSpuList"
          />
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script lang="ts">
export default {
  name: "Spu-list",
};
</script>

<script lang="ts" setup>
import { ref, watch } from "vue";
import { Plus, Edit, ZoomIn, Delete } from "@element-plus/icons-vue";
import type { SpuList, SpuItem } from "@/api/product/model/spuModel";
import { getSpuPageApi } from "@/api/product/spu";
import { useCategoryStore } from "@/stores/category";
import { useSpuStore } from "@/stores/spu";
const emit = defineEmits(["setIsShow"]);
const spuStore = useSpuStore();
// 显示添加Spu
const isShowAddSpu = () => {
  spuStore.$reset();
  emit("setIsShow", 2);
};
// 显示更新spu
const isShowUpdateSpu = (row: SpuItem) => {
  const { id, spuName, tmId, description } = row;
  spuStore.$patch({
    id,
    spuName,
    tmId,
    description,
  });
  emit("setIsShow", 2);
};
const isShowAddSku = (row: SpuItem) => {
  const { id, spuName } = row;
  spuStore.$patch({
    id,
    spuName,
  }),
    emit("setIsShow", 3);
};
const categoryStore = useCategoryStore();
const currentPage = ref(1);
const pageSize = ref(3);
const total = ref(0);
const spuList = ref<SpuList>([]);

const getSpuList = async () => {
  const res = await getSpuPageApi({
    page: currentPage.value,
    limit: pageSize.value,
    category3Id: categoryStore.category3Id as number,
  });

  spuList.value = res.records;
  total.value = res.total;
};

watch(
  () => categoryStore.category3Id,
  (category3Id) => {
    if (!category3Id) {
      spuList.value = [];
      currentPage.value = 1;
      pageSize.value = 3;
      total.value = 0;
      return;
    }
    getSpuList();
  },
  { immediate: true }
);
</script>

<style scoped></style>
